import { Global, Module } from '@nestjs/common';
import { DbService } from './db.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigureModule, ConfigureService } from '@app/configure';
import { BaseModule } from './base/base.module';
import { MemberModule } from './common/member/member.module';
import { CompanyModule } from './common/company/company.module';
import { LocaleModule } from './common/locale/locale.module';
import { LangModule } from './common/lang/lang.module';
import { MenuModule } from './common/menu/menu.module';
import { DictionaryCategoryModule } from './common/dictionary-category/dictionary-category.module';
import { DictionaryItemModule } from './common/dictionary-item/dictionary-item.module';
import { RoleModule } from './common/role/role.module';
import { TenantModule } from './common/tenant/tenant.module';
import { DataSource } from 'typeorm';
import { IotSimModule } from './iot-sim/iot-sim.module';
import { DeviceModule } from './device/device.module';
import { PlatformDataModule } from './common/platform-data/platform-data.module';
import { PricingSlowChargeModule } from './pricing-slow-charge/pricing-slow-charge.module';
import { PricingFastChargeModule } from './pricing-fast-charge/pricing-fast-charge.module';
import { DeviceSettingAttrModule } from './device-setting-attr/device-setting-attr.module';
import { SettingAttrModelModule } from './setting-attr-model/setting-attr-model.module';
import { DeviceModelModule } from './device-model/device-model.module';
import { DeviceParamsRuleModule } from './device-params-rule/device-params-rule.module';
import { ChargeOrderModule } from './charge-order/charge-order.module';
import { RechargeOptionModule } from './recharge-option/recharge-option.module';
import { RechargeOrderModule } from './recharge-order/recharge-order.module';
import { UserModule } from './user/user.module';
import { MerchantModule } from './merchant/merchant.module';
import { ChargingStationModule } from './charging-station/charging-station.module';
import { MerchantDailyIncomeModule } from './merchant-daily-income/merchant-daily-income.module';
import { WithdrawalRecordModule } from './withdrawal-record/withdrawal-record.module';
import { PlatformDailyIncomeModule } from './platform-daily-income/platform-daily-income.module';
import { PlatformModule } from './platform/platform.module';
import { FirmwareModule } from './firmware/firmware.module';
import { OtaTaskModule } from './ota-task/ota-task.module';
import { OtaDeviceModule } from './ota-device/ota-device.module';
import { ChargingCardModule } from './charging-card/charging-card.module';
import { CardRechargeOrderModule } from './card-recharge-order/card-recharge-order.module';

@Global()
@Module({
  imports : [
    TypeOrmModule.forRootAsync({
      name : 'common',
      imports : [ConfigureModule],
      inject : [ConfigureService],
      useFactory : (config:ConfigureService) => {
        return {
          ...config.get('database.common'),
        }
      },

      dataSourceFactory: async (options) => {
        const dataSource = await new DataSource(options).initialize();
        return dataSource;
      },
    }),

    TypeOrmModule.forRootAsync({
      imports : [ConfigureModule],
      inject : [ConfigureService],
      useFactory : (config:ConfigureService) => {
        return {
          ...config.get('database.default'),
        }
      },

      dataSourceFactory: async (options) => {
        const dataSource = await new DataSource(options).initialize();
        return dataSource;
      },
    }),


    BaseModule,
    MemberModule,
    CompanyModule,
    LocaleModule,
    LangModule,
    MenuModule,
    DictionaryCategoryModule,
    DictionaryItemModule,
    RoleModule,
    TenantModule,
    IotSimModule,
    DeviceModule,
    PlatformDataModule,
    PricingSlowChargeModule,
    PricingFastChargeModule,
    DeviceSettingAttrModule,
    SettingAttrModelModule,
    DeviceModelModule,
    DeviceParamsRuleModule,
    ChargeOrderModule,
    RechargeOptionModule,
    RechargeOrderModule,
    UserModule,
    MerchantModule,
    ChargingStationModule,
    MerchantDailyIncomeModule,
    WithdrawalRecordModule,
    PlatformDailyIncomeModule,
    PlatformModule,
    FirmwareModule,
    OtaTaskModule,
    OtaDeviceModule,
    ChargingCardModule,
    CardRechargeOrderModule,
  ],
  providers: [DbService],
  exports: [DbService],
})
export class DbModule {}
